package com.atguigu.gulimall.order.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AbstractMessageConverter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Slf4j
@Configuration
public class MyRabbitConfig {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Bean
    public AbstractMessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /***
     * 定制RabbitTemplate
     * 1.服务收到消息就回调
     *    1.spring.rabbitmq.publisher-confirms=true
     *    2.设置确认回调ConfirmCallback
     * 2.消息正确抵达队列进行回调
     *    1.spring.rabbitmq.publisher-returns=true
     *      spring.rabbitmq.template.mandatory=true
     *    2.设置确认回调ReturnCallback
     * 3.消费端确认(保证每个消息被正确消费,此时才可以broker删除这个消息)
     *    1.默认是自动确认的,只要消息接收到,客户端会自动确认,服务端就会移除这个消息
     *      问题:
     *          我们收到很多消息,自动回复给服务器ack,只有一个消息处理成功,宕机了,发生消息丢失
     *      解决方法:手动确认
     *          只要我们没有明确告诉MQ货物被签收(没有ack),消息就一直是unacked状态,即使Consumer宕机,
     *          消息不会丢失,会重新变为Ready,下一次有新的Consumer连接进来就发给它
     *          如何签收?
     *          channel.basicAck(deliveryTag,false); 签收:业务成功完成就应该签收
     *          channel.basicNack(deliveryTag,false,true); 拒签:业务失败,拒签
     */

    @PostConstruct // 当MyRabbitConfig对象创建完成之后,执行此方法,初始化重构rabbitTemplate模版的confirm方法
    public void initRabbitTemplate(){
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            /**
             * @param correlationData 关联数据也就是消息唯一id
             * @param b ack:broker是否接收到消息
             * @param s cause:失败的原因
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                log.info("correlationData:" + correlationData + "," + "b: " + b + " , " + s);
            }
        });
        // 当交换机未将消息传递到队列时的回调函数
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback(){
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                // 获取路由key
                String routingKey = returnedMessage.getRoutingKey();
                // 获取交换机
                String exchange = returnedMessage.getExchange();
                // 发送的消息
                String message = new String(returnedMessage.getMessage().getBody());
                // 发送的错误信息提示
                String replyText = returnedMessage.getReplyText();

                log.info("routingKey" + routingKey +
                        "exchange" + exchange +
                        "message" + message +
                        "replyText" + replyText);
            }
        });


    }

}
